package org.hhgzs.modules.erp.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.entity.Account;
import org.hhgzs.modules.erp.entity.Depot;
import org.hhgzs.modules.erp.service.AccountService;
import org.hhgzs.modules.erp.service.DepotService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账户信息 前端控制器
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/account")
@Slf4j
@RequiredArgsConstructor
@Api("结算账户管理")
public class AccountController extends JeecgController<Account, AccountService> {

    private final AccountService accountService;
    /**
     * 获取用户列表数据
     *
     * @param data
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @PermissionData(pageComponent = "basedata/DepotList")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("列表查询")
    public Result<IPage<Account>> queryPageList(Account data, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<Account>> result = new Result<IPage<Account>>();
        QueryWrapper<Account> queryWrapper = QueryGenerator.initQueryWrapper(data, req.getParameterMap());

        Page<Account> page = new Page<Account>(pageNo, pageSize);
        IPage<Account> pageList = accountService.page(page, queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }


    /**
     * 获取所有结算账户
     * @param request
     * @return
     */
    @GetMapping(value = "/getAccount")
    public Result getAccount(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Account> accountList = accountService.getAccount();
            map.put("accountList", accountList);
            return Result.OK(map);
        } catch(Exception e){
            e.printStackTrace();
            return Result.error("获取数据失败");
        }
    }

    /**
     * update
     *
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public Result<?> delete(@RequestParam(required = true) Serializable id) {
        boolean b = accountService.removeById(id);
        if(b){
            return Result.OK().success("删除成功!");
        }else{
            return Result.OK(false);
        }
    }

    @PostMapping(value = "/updateIsDefault")
    public Result updateIsDefault(@RequestBody JSONObject object,
                                  HttpServletRequest request) throws Exception{
        Long depotId = object.getLong("id");
        Map<String, Object> objectMap = new HashMap<>();
        int count = accountService.updateIsDefault(depotId);
        if(count>0){
            return Result.OK(true);
        }else{
            return Result.OK(false);
        }
    }
}
